Skip to content

[Dataflow Streaming] Prepare BoundedQueueExecutor for MultiKey bundles#38592

Open
arunpandianp wants to merge 4 commits into
apache:masterfrom
arunpandianp:multikey4
Open

[Dataflow Streaming] Prepare BoundedQueueExecutor for MultiKey bundles#38592
arunpandianp wants to merge 4 commits into
apache:masterfrom
arunpandianp:multikey4

Conversation

@arunpandianp
Copy link
Copy Markdown
Contributor

@arunpandianp arunpandianp commented May 22, 2026

Adds a getter that will be used by StreamingWorkScheduler/Context to pull more work from the queue when executing a multi key bundle. The queuing logic will be be improved and getter signature will change in a later PR.

@arunpandianp
Copy link
Copy Markdown
Contributor Author

R: @scwhittle

@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request prepares the BoundedQueueExecutor for multi-key bundles by updating the work execution lifecycle. By introducing a WorkResult object, the system can now accurately track and decrement resource budgets (items and bytes) after work execution. The changes include refactoring existing interfaces to support this return type, improving error handling via a new utility class, and ensuring that resource budgets are correctly reclaimed even in the event of task submission failures.

Highlights

  • Introduced WorkResult: Added a new WorkResult class to encapsulate items and bytes processed, allowing for more granular resource management during work execution.
  • Refactored ExecutableWork: Updated ExecutableWork to return a WorkResult instead of using a Consumer, enabling better tracking of resource usage.
  • Enhanced BoundedQueueExecutor: Updated BoundedQueueExecutor to process WorkResult objects, ensuring accurate resource budget reclamation and handling potential execution failures.
  • Added ExceptionUtils: Introduced a utility class to safely propagate exceptions, ensuring consistent error handling across the worker.
New Features

🧠 You can now enable Memory (public preview) to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize the Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counterproductive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@github-actions
Copy link
Copy Markdown
Contributor

Stopping reviewer notifications for this pull request: review requested by someone other than the bot, ceding control. If you'd like to restart, comment assign set of reviewers

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors the streaming worker's execution logic by introducing a WorkResult class to track processed items and bytes. ExecutableWork is updated to return this result, and BoundedQueueExecutor is modified to manage resource budgets using WorkResult instead of raw byte counts. Additionally, an ExceptionUtils utility is added for standardized exception propagation. A critical review comment identifies a potential resource leak in BoundedQueueExecutor where the executeMonitorHeld method for ExecutableWork lacks error handling for task submission and execution failures, which could lead to permanent capacity leaks in the JVM if not addressed.

Comment on lines +236 to +245
executor.execute(
() -> {
// Any execution exception thrown by work.run() propagates uncaught, triggering
// the default JVM UncaughtExceptionHandler which immediately crashes/terminates
// the JVM. Since the process exits immediately, reclaiming resource budgets in
// this JVM is unnecessary. Furthermore, since a failed execution does not return
// a WorkResult, we do not have a good/accurate fallback value to decrement.
WorkResult result = work.run();
decrementCounters(result);
});
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The executeMonitorHeld method for ExecutableWork lacks error handling for task submission and execution failures, which can lead to resource leaks in elementsOutstanding and bytesOutstanding.

  1. If executor.execute() throws a RejectedExecutionException (e.g., during worker shutdown), the counters incremented just before will never be decremented, leading to a permanent capacity leak in the JVM.
  2. If work.run() throws an exception and the JVM does not immediately terminate (e.g., if the exception is caught by the thread pool's uncaught exception handler but the process continues), the counters will also leak.

This implementation should be made consistent with the Runnable version of executeMonitorHeld (lines 248-272), which correctly handles these scenarios. Even if a JVM crash is expected on work failure, providing a defensive decrement (using the initial workBytes as a fallback) is safer.

    try {
      executor.execute(
          () -> {
            WorkResult result = null;
            try {
              result = work.run();
            } finally {
              // Any execution exception thrown by work.run() propagates uncaught, triggering
              // the default JVM UncaughtExceptionHandler which immediately crashes/terminates
              // the JVM. Since the process exits immediately, reclaiming resource budgets in
              // this JVM is unnecessary. Furthermore, since a failed execution does not return
              // a WorkResult, we fallback to decrementing the initial budget to prevent
              // resource leaks in case the JVM does not crash.
              decrementCounters(result != null ? result : WorkResult.create(1, workBytes));
            }
          });
    } catch (Throwable e) {
      // If the execute() call threw an exception, decrement counters here.
      decrementCounters(WorkResult.create(1, workBytes));
      throw ExceptionUtils.propagate(e);
    }

@arunpandianp arunpandianp marked this pull request as draft May 22, 2026 10:27
@arunpandianp
Copy link
Copy Markdown
Contributor Author

Changing a few things, not ready for review.

@arunpandianp arunpandianp marked this pull request as ready for review May 22, 2026 22:44
@arunpandianp
Copy link
Copy Markdown
Contributor Author

/gemini review

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a mechanism for inline work execution within the BoundedQueueExecutor by utilizing a new BoundedQueueExecutorWorkHandle interface. Key changes include refactoring ExecutableWork to support this handle, adding a pollWork method to BoundedQueueExecutor for retrieving queued tasks, and implementing a Budget tracking system to manage outstanding elements and bytes during batch processing. Feedback was provided to ensure the close() method in the handle implementation is idempotent to comply with the AutoCloseable contract and to implement safer type checking when casting the handle in the pollWork method.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a mechanism for polling and executing work inline within the BoundedQueueExecutor by implementing a new BoundedQueueExecutorWorkHandle interface. Key changes include refactoring ExecutableWork to support these handles and updating the executor to track outstanding work using a Budget object that accounts for both element counts and byte sizes. Feedback suggests making the close() method in the handle implementation idempotent to avoid masking exceptions and adding a type check in pollWork to prevent potential ClassCastException when casting the handle.

@arunpandianp
Copy link
Copy Markdown
Contributor Author

/gemini review

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a mechanism to poll and execute work items inline within the BoundedQueueExecutor, facilitating better control over work budget and resource management. Key changes include the introduction of BoundedQueueExecutorWorkHandle and ExecutableWork to track work budgets, and the addition of a pollWork method to allow worker threads to pull additional tasks from the queue. Feedback was provided regarding the pollWork method's behavior of executing non-QueuedWork items on the calling thread, which could potentially impact latency if those tasks are long-running.

Comment on lines +361 to 374
while (true) {
Runnable runnable = executor.getQueue().poll();
if (runnable == null) {
return Optional.empty();
}
if (runnable instanceof QueuedWork) {
QueuedWork queuedWork = (QueuedWork) runnable;
queuedWork.cancelHandle();
internalHandle.addBudget(1, queuedWork.getWorkBytes());
return Optional.of(queuedWork.getWork());
}
// Pop and execute standard callbacks immediately on the calling thread to drain the queue
runnable.run();
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The pollWork method executes any non-QueuedWork items (such as callbacks submitted via forceExecute(Runnable)) directly on the calling thread to "drain the queue" while searching for the next QueuedWork. This is a significant change in behavior, as these tasks were previously always executed by the executor's thread pool. If these callbacks are long-running or perform blocking operations, they could delay the completion of the current work item and the release of its associated budget. While this is necessary to reach items further back in the LinkedBlockingQueue, it should be monitored for potential impact on worker thread latency.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The getter is unused now. Planning to improve the logic before using it. This change is just setting up the Handles and getters.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant